package me.green.ui.event;

import me.green.bean.Diary;
import me.green.dao.DiaryDao;
import me.green.dao.PlanDao;
import me.green.ui.LocalCache;
import me.green.ui.EtnFrame;
import me.green.ui.global.UIStyle;
import me.green.util.DateUtil;
import me.green.util.HtmlBuilder;
import me.green.util.URLAnalysisUtil;

import javax.swing.*;

public class EditorPaneLinkEvent {

    public final static int METHOD_DIARY = 1;
    public final static int METHOD_PLAN = 2;

    private JEditorPane browser;

    public EditorPaneLinkEvent(String url, JEditorPane browser, int method) {
        this.browser = browser;
        int id = URLAnalysisUtil.getIDFromURLOfEditorPane(url);
        String op = URLAnalysisUtil.getOpTypeFromURLOfEditorPane(url);

        if (op.equals("detail")) {
            if (method == EditorPaneLinkEvent.METHOD_DIARY) {
                showDiaryDetailRequest(id, browser);
            }
        } else if (op.equals("edit")) {
            if (method == EditorPaneLinkEvent.METHOD_DIARY) {
                editDiaryRequest(id);
            }
        } else if (op.equals("delete")) {
            if (method == EditorPaneLinkEvent.METHOD_DIARY) {
                deleteDiaryRequest(id);
            }
            if (method == EditorPaneLinkEvent.METHOD_PLAN) {
                deletePlan(id);
            }
        } else if (op.equals("archive")) {
            achievedPlanRequest(id);
        }

    }

    private void showDiaryDetailRequest(int id, JEditorPane browser) {
        Diary diary = new DiaryDao().getCompletedDiaryById(id);
        browser.setText(HtmlBuilder.genDiaryDetailHtmlString(diary));
    }

    private void editDiaryRequest(int id) {
        //new ModDiaryDialog(LocalCache.getFrameApp(), true, id);
    }

    private void deleteDiaryRequest(int id) {
        int flag = JOptionPane.showConfirmDialog(LocalCache
                .getFrameApp(), UIStyle.text("tip.delete.confirm"));
        if (flag == JOptionPane.OK_OPTION) {
            boolean delFlag = new DiaryDao().delCompletedDiaryById(id);
            if (delFlag) {
                JOptionPane.showMessageDialog(LocalCache.getFrameApp(),
                        "删除成功！");
                LocalCache.getFrameApp().displayEditorPane(
                        (java.sql.Date) DateUtil.getWeekBegin(0),
                        (java.sql.Date) DateUtil.getWeekEnd((java.sql.Date) DateUtil.getWeekBegin(0)), browser,
                        EtnFrame.AreaType.DIARY);
            } else {
                JOptionPane.showMessageDialog(LocalCache.getFrameApp(),
                        "删除失败！", UIStyle.text("tip"), 0);
            }
        }
    }

    /**
     * 计划删除
     *
     * @param id
     */
    private void deletePlan(int id) {
        int flag = JOptionPane.showConfirmDialog(LocalCache.getFrameApp(),
				UIStyle.text("tip.delete.confirm"));
        if (flag == JOptionPane.OK_OPTION) {
            boolean delFlag = new PlanDao().delete(id);
            if (delFlag) {
                JOptionPane.showMessageDialog(LocalCache.getFrameApp(),
                        UIStyle.text("success.delete"));
                LocalCache.getFrameApp().updateEditorPaneContent();
            } else {
                JOptionPane.showMessageDialog(LocalCache.getFrameApp(),
                        UIStyle.text("error.delete"), UIStyle.text("tip"), 0);
            }
        }
    }

    /**
     * 计划归档
     *
     * @param id
     */
    private void achievedPlanRequest(int id) {
        int flag = JOptionPane.showConfirmDialog(LocalCache.getFrameApp(),
                UIStyle.text("tip.archive.confirm"));
        if (flag == JOptionPane.OK_OPTION) {
            new PlanDao().changeState(id, "2");
			LocalCache.getFrameApp().updateEditorPaneContent();
        }
    }
}
